/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;



import Offer.Offer;
import Offer.OfferHandler;
import Offer.OfferTableModel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.xml.stream.XMLStreamException;

/**
 *
 * @author Dopsilon
 */
public class GUImain {
    private JFrame mainWindow;
    private JDesktopPane desktop;  
    
    private GUIMenu mainMenu;
    
    
    public GUImain(){
        buildFrame();  
    
        
       // buildOfferTable();
        //this.mainWindow.setJMenuBar(mainMenu.getMenuBar());
        this.mainWindow.setVisible(true);
        
    }
    
    
    
    
    private void buildFrame(){
        this.mainWindow = new JFrame("Travel info");
        this.mainMenu = new  GUIMenu(this);
        int indent = 75;
        Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
        this.mainWindow.setResizable(false);
        this.mainWindow.setBounds(indent, indent,
                screenDimension.width-indent*2,
                screenDimension.height-indent*2);
        
        this.desktop = new JDesktopPane();
        
        
        
        this.mainWindow.setContentPane(this.desktop);
        this.mainWindow.setJMenuBar(mainMenu.getMenuBar());
        
            buildInfoPanel();
        
       
        this.mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        
       
    }
    
    
    
    protected void buildInfoPanel(){
        try {
            InternalPanels infoFrame = new InternalPanels("Info");
            infoFrame.setSize(2*(this.mainWindow.getWidth()/5),
                    this.mainWindow.getHeight()-50);
            
            infoFrame.setLocation((this.mainWindow.getWidth()-infoFrame.getWidth())-1,
                    0);
            
            infoFrame.add(textPane());
            this.desktop.add(infoFrame);
            infoFrame.toFront();
            infoFrame.setVisible(true);
        } catch (InternalPanelException ex) {
            printCriticalException(ex);
        }
        
    }
    
    protected JPanel textPane(){
        JPanel textPane = new JPanel();
        textPane.setLayout(new BorderLayout());      
        
        JTextPane infoArea = new JTextPane();
        infoArea.setAutoscrolls(true);
        textPane.add(infoArea, BorderLayout.CENTER);     
        
        
        return textPane;
    }
    
    
    
    protected void buildOfferTable(){ 
        try {
            InternalPanels tableFrame = new InternalPanels("Table");
            tableFrame.setVisible(true);
            tableFrame.add(buildTable());
            this.desktop.add(tableFrame);      
        } catch (InternalPanelException ex) {
            printCriticalException(ex);
        }
        
    }
    
    private JPanel buildTable(){
        /*
        String[] columns = {"Mikael", "Är", "Bäst","Info Button"};
        Object[][] data = {
            {"Ja", "Ja", "Ja", new JButton("INFO")}
        };*/
        
        JPanel travelData = new JPanel();
        travelData.setLayout(new BorderLayout());
        JTable dataValues;
        try {
            dataValues = new JTable(new OfferTableModel());
             dataValues.setDragEnabled(false);
             JScrollPane sp = new JScrollPane(dataValues);
        travelData.add(sp, BorderLayout.CENTER);
        } catch (FileNotFoundException | XMLStreamException | ParseException ex) {
            printCriticalException(ex);
        }
       
        
        
        return  travelData;
        
    }
    
    protected void quit(){
        System.exit(0);
    }
    
    protected JPanel options(){
        JPanel optionsPanel = new JPanel();
        optionsPanel.setLayout(new BorderLayout());
        
        return optionsPanel;
    }

    protected void optionsPane() {
        try {
            InternalPanels optionsFrame = new InternalPanels("Options");
            optionsFrame.add(options());
            this.desktop.add(optionsFrame);
            
            optionsFrame.setLocation((this.mainWindow.getWidth()-optionsFrame.getWidth())/2,
                    (this.mainWindow.getHeight()-optionsFrame.getHeight())/2);
            optionsFrame.toFront();
            optionsFrame.setVisible(true);
        } catch (InternalPanelException ex) {
            printCriticalException(ex);
        }
       
    }
    

    public void printCriticalException(java.lang.Exception ex) {
        JOptionPane.showMessageDialog(this.mainWindow, ex.getLocalizedMessage(),
                "Critiical Error", ERROR_MESSAGE);
    }
    
    public void printSituationalException(java.lang.Exception ex) {
        JOptionPane.showMessageDialog(this.mainWindow, ex.getLocalizedMessage(),
                "Situational Error", ERROR_MESSAGE);
    }
}
